﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Soothe
{
    // throw away any velocities older than six months
    public class HistoryOfVelocities : List<CompletedTask>
    {
        private DateTime _minDateTime = DateTime.Now.AddMonths(-6);

        /*internal*/
        public void GenerateFakeHistory()
        {
            var random = new Random();

            for (var i = 0; i < 1000; i++)
                this.Add(new CompletedTask(_minDateTime, 0.5 + (random.Next(15 + 1) / 10d)));
        }

        private List<CompletedTask> _lastVelocities()
        {
            throw new NotImplementedException();

            //return this
            //    .Where(task => task.TimeStamp.Subtract(_minDateTime) > new TimeSpan())
            //    .ToList();
        }

        private List<CompletedTask> _randomCompletedTasks()
        {
            var random = new Random();
            var randomCompletedTasks = new CompletedTask[100];

            for (var i = 0; i < 100; i++)
            {
                var task = this[random.Next(this.Count)];

                randomCompletedTasks[i] = task;
            }

            return randomCompletedTasks.ToList();
        }

        private List<TimeSpan> SimulateTheFuture(int estimatedHours, IEnumerable<CompletedTask> tasks)
        {
            return tasks
                .Select(task => TimeSpan.FromHours(Math.Round(estimatedHours / task.Velocity, 1)))
                .ToList();
        }

        public List<TimeSpan> SimulateTheFuture(int estimatedHours)
        {
            return this.SimulateTheFuture(estimatedHours, _randomCompletedTasks());
        }

        public List<Tuple<TimeSpan, double>> ProbabilityDensities(IEnumerable<TimeSpan> possibleFutures)
        {
            double percent = 0;

            return possibleFutures
                .OrderBy(future => future)
                .GroupBy(
                    future => future,
                    (future, futures) => Tuple.Create(
                        future,
                        percent += futures.Count() / (double)possibleFutures.Count())
                        )
                .ToList();
        }
        public IEnumerable<DateTime> HoursToSchedule(IEnumerable<decimal> possibleFutures)
        {
            throw new NotImplementedException();
        }
    }
}
